package young.Util;




import java.io.*;
import java.text.DecimalFormat;
import java.util.Properties;


/**
 * 工具类
 */
public class Util {

    /**
     * 随机数工具
     *
     * @param start 初始值
     * @param end   最大值
     * @return 整型随机数
     */

    public static int radDom(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }

    /**
     * 驼峰命名工具
     */
    public static String snakeToCamel(String name) {
        String[] names = name.split("_");
        String str = "";
        for (int i = 0; i < names.length; i++) {
            str += Character.toUpperCase(names[i].charAt(0)) + names[i].substring(1);
        }
        return str;
    }

    /**
     * 蛇形命名工具
     */
    public static String camelToSnake(String name) {
        String newName = "";
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (Character.isUpperCase(c) && i > 0) {
                newName += "_" + c;
            } else {
                newName += c;
            }

        }
        return newName.toLowerCase();
    }

    /**
     * 千分位工具
     *
     * @param
     * @return
     */
    public static String currency(double d) {
        DecimalFormat decimalFormat = new DecimalFormat(",###.00");
        return decimalFormat.format(d);
    }

    /**
     * 电脑出拳工具
     *
     * @param value
     * @return
     */
    public static String showResult(int value) {
        String result = "";
        switch (value) {
            case 0:
                result = "剪刀";
                break;
            case 1:
                result = "石头";
                break;
            case 2:
                result = "布";
                break;
        }
        return result;
    }
    /**
     * properties输出工具
     *
     * @param path       保存路径
     * @param properties properties对象
     * @param comments   注释
     */
    public static void proFileOutput(String path, Properties properties, String comments) {

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(path);
            properties.store(fos, comments);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * Properties 提取工具
     *
     * @param path 文件路径
     * @return 返回 properties对象
     */
    public static Properties propertiesInput(String path) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            Properties properties = new Properties();
            properties.load(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        return null;
    }

    /**
     * 序列化对象
     *
     * @param path 保存序列化对象的路径
     * @param obj  序列化对象
     */




    public static void writeObject(String path, Object obj) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;

        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    /**
     * @param path 反序列化对象地址
     * @return
     */
    public static Object readObject(String path) {
        FileInputStream fis=null;
        ObjectInputStream ois = null;
        try {
            fis=new FileInputStream(path);
            ois=new ObjectInputStream(fis);
            return ois.readObject();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return null;
    }


}








